package com.example.xm.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.example.xm.entity.Daan;
import com.example.xm.entity.Fenlei;
import com.example.xm.entity.Timu;
import com.example.xm.entity.Udaan;
import com.example.xm.mapper.DaanMapper;
import com.example.xm.mapper.TimuMapper;
import com.example.xm.mapper.UdaanMapper;
import com.example.xm.service.ITimuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class TimuServiceImpl implements ITimuService
{
    @Autowired
    private TimuMapper timuMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Timu selectTimuById(Long id)
    {
        return timuMapper.selectTimuById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param timu 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Autowired
    private DaanMapper daanMapper;
    @Autowired
    private UdaanMapper udaanMapper;
    @Override
    public List<Timu> selectTimuList(Timu timu)
    {
        List<Timu> timus = timuMapper.selectTimuList(timu);
        List<Daan> daans = daanMapper.selectDaanList(null);
        Map<Long, List<Daan>> collect = daans.stream().collect(Collectors.groupingBy(Daan::getTid));
        for (Timu timus1 : timus) {
            List<Daan> daans1 = collect.get(timus1.getId());
            timus1.setList(daans1);
        }
        Udaan udaan = new Udaan();
        udaan.setVid(timu.getVid());
        udaan.setUid(timu.getUid());
        List<Udaan> udaans = udaanMapper.selectUdaanList(udaan);
        //考过了
        if(udaans.size()>0){
            Map<Long, List<Udaan>> collect1 = udaans.stream().collect(Collectors.groupingBy(Udaan::getTid));
            for (Timu timus1 : timus) {
                List<Udaan> udaans1 = collect1.get(timus1.getId());
                timus1.setUdaan(udaans1.get(0).getUdaan());
                timus1.setStatus(udaans1.get(0).getStatus());
            }
        }
        return timus;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param timu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTimu(Timu timu)
    {
        return timuMapper.insertTimu(timu);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param timu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTimu(Timu timu)
    {
        return timuMapper.updateTimu(timu);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTimuByIds(Long[] ids)
    {
        return timuMapper.deleteTimuByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTimuById(Long id)
    {
        return timuMapper.deleteTimuById(id);
    }

    @Override
    public  PageInfo<Timu> selectTimuListt(Timu timu,Integer page,Integer size) {
        PageHelper.startPage(page,size);
        List<Timu> timus = timuMapper.selectTimuList(timu);
        PageInfo<Timu> adminPageInfo = new PageInfo<Timu>(timus);
        return adminPageInfo;
    }

    @Override
    public void delete(int[] intArray) {
        timuMapper.delete(intArray);
    }
}
